package library.view;

import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Iterator;
import java.util.List;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;

import library.controller.LibraryControllerListener;
import library.pojo.User;
import library.service.LibraryService;

@SuppressWarnings("serial")
public class ManageJPanel extends JPanel {
	private JTextField bookManageTextField;
	private JTable table;
	private JScrollPane tablePanel;
	private JComboBox searchTypeComboBox;
	private JScrollPane scrollPane;
	private LibraryService libraryService;
	private List<User> userList;
	private User user;
	private int selRow = -1;
	private ImageIcon icon;
	private Image img;

	public void paintComponent(Graphics g) {
		super.paintComponent(g);
		g.drawImage(img,0,0,this.getWidth(),this.getHeight(),this);
	}

	public ManageJPanel(List<User> userList, LibraryControllerListener libraryControllerListener) {
		setBounds(100, 100, 1200, 750);
		this.setLayout(null);
		this.userList = userList;

		icon = new ImageIcon("res/2.png");
		img = icon.getImage();
		
		JLabel message = new JLabel("请选择查询类型:");
		message.setFont(new Font("宋体", Font.PLAIN, 18));
		message.setBounds(189, 32, 248, 18);
		this.add(message);

		String[] ways = { "按照用户类型查询", "按照用户名查询","按照用户姓名查询" };
		searchTypeComboBox = new JComboBox(ways);
		searchTypeComboBox.setFont(new Font("宋体", Font.PLAIN, 14));
		searchTypeComboBox.setToolTipText("");
		searchTypeComboBox.setBounds(189, 63, 248, 24);
		this.add(searchTypeComboBox);

		bookManageTextField = new JTextField();
		bookManageTextField.setBounds(514, 63, 295, 24);
		this.add(bookManageTextField);
		bookManageTextField.setColumns(10);

		JButton searchButton = new JButton("查询");
		searchButton.setIcon(new ImageIcon("res/search.png"));
		searchButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				bookSearchActionPerformed(e);
			}
		});
		searchButton.setActionCommand("searchPost");
		searchButton.setFont(new Font("宋体", Font.PLAIN, 18));
		searchButton.setBounds(830, 62, 99, 27);
		this.add(searchButton);

		JButton addButton = new JButton("添加");
		addButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				addActionPerformed(e);
			}
		});
		addButton.setFont(new Font("宋体", Font.PLAIN, 18));
		addButton.setBounds(231, 621, 113, 27);
		this.add(addButton);

		JButton deleteButton = new JButton("删除");
		deleteButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				deleteActionPerformed(e);
			}
		});
		deleteButton.setFont(new Font("宋体", Font.PLAIN, 18));
		deleteButton.setBounds(498, 621, 113, 27);
		this.add(deleteButton);

		JButton updateButton = new JButton("修改");
		updateButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				updateActionPerformed(e);
			}
		});
		updateButton.setFont(new Font("宋体", Font.PLAIN, 18));
		updateButton.setBounds(775, 621, 113, 27);
		this.add(updateButton);

		scrollPane = new JScrollPane();
		scrollPane = this.getContactJPanel(userList);
		scrollPane.setBounds(189, 120, 740, 478);
		this.add(scrollPane);

}

	/**
	 * @param 删除
	 */
	protected void deleteActionPerformed(ActionEvent e) {
		if(this.user == null) {
			JOptionPane.showMessageDialog(null, "请选择一位用户的信息！");
			return;
		}
		DeleteUserDialog dialog = new DeleteUserDialog("删除用户信息",user);
		
		selRow = -1;
		userList = LibraryService.findAllUser();
		String name[] = { "用户名", "密码", "姓名", "联系电话", "用户类型" };
		Object data[][] = new Object[userList.size()][5];
		Iterator<User> iter = userList.iterator();
		int i = 0;
		while (iter.hasNext()) {
			User user = iter.next();
			data[i][0] = user.getUsername();
			data[i][1] = user.getPassword();
			data[i][2] = user.getName();
			data[i][3] = user.getPhone();
			data[i][4] = user.getType();
			i++;
		}
		DefaultTableModel tableModel = new DefaultTableModel(data, name);
		table = new JTable(tableModel);
		table.getTableHeader().setFont(new Font("宋体", Font.BOLD, 16)); // 表头字体
		table.setFont(new Font("微软雅黑", Font.PLAIN, 14)); // 表体字体
		table.getTableHeader().setPreferredSize(new Dimension(1, 30)); // 表头高度
		table.setRowHeight(table.getRowHeight() + 15); // 表体高度
		table.repaint();
		tablePanel = new JScrollPane(table);
		table.addMouseListener(new MouseAdapter() {
			public void mouseClicked(final MouseEvent e) {
				selRow = table.getSelectedRow(); // 得到选中的行
				if (selRow >= 0) {
					user = new User(userList.get(selRow).getUsername(),userList.get(selRow).getPassword(),userList.get(selRow).getName(),userList.get(selRow).getType(),userList.get(selRow).getPhone());
				}
			}

		});

		this.remove(scrollPane);
		scrollPane = tablePanel;
		scrollPane.setBounds(189, 120, 740, 478);
		this.add(scrollPane);
		this.validate();
	}

	/**
	 * @param 修改
	 */
	protected void updateActionPerformed(ActionEvent e) {
		if(this.user == null) {
			JOptionPane.showMessageDialog(null, "请选择一位用户的信息！");
			return;
		}
		UpdateUserDialog dialog = new UpdateUserDialog("修改用户信息",user);
		selRow = -1;
		userList = LibraryService.findAllUser();
		String name[] = { "用户名", "密码", "姓名", "联系电话", "用户类型" };
		Object data[][] = new Object[userList.size()][5];
		Iterator<User> iter = userList.iterator();
		int i = 0;
		while (iter.hasNext()) {
			User user = iter.next();
			data[i][0] = user.getUsername();
			data[i][1] = user.getPassword();
			data[i][2] = user.getName();
			data[i][3] = user.getPhone();
			data[i][4] = user.getType();
			i++;
		}
		DefaultTableModel tableModel = new DefaultTableModel(data, name);
		table = new JTable(tableModel);
		table.getTableHeader().setFont(new Font("宋体", Font.BOLD, 16)); // 表头字体
		table.setFont(new Font("微软雅黑", Font.PLAIN, 14)); // 表体字体
		table.getTableHeader().setPreferredSize(new Dimension(1, 30)); // 表头高度
		table.setRowHeight(table.getRowHeight() + 15); // 表体高度
		table.repaint();
		tablePanel = new JScrollPane(table);
		table.addMouseListener(new MouseAdapter() {
			public void mouseClicked(final MouseEvent e) {
				selRow = table.getSelectedRow(); // 得到选中的行
				if (selRow >= 0) {
					user = new User(userList.get(selRow).getUsername(),userList.get(selRow).getPassword(),userList.get(selRow).getName(),userList.get(selRow).getType(),userList.get(selRow).getPhone());
}
			}

		});

		this.remove(scrollPane);
		scrollPane = tablePanel;
		scrollPane.setBounds(189, 120, 740, 478);
		this.add(scrollPane);
		this.validate();
	}

	/**
	 * @param 添加
	 */
	protected void addActionPerformed(ActionEvent e) {
		AddUserDialog dialog = new AddUserDialog();

		selRow = -1;
		userList = LibraryService.findAllUser();
		String name[] = { "用户名", "密码", "姓名", "联系电话", "用户类型" };
		Object data[][] = new Object[userList.size()][5];
		Iterator<User> iter = userList.iterator();
		int i = 0;
		while (iter.hasNext()) {
			User user = iter.next();
			data[i][0] = user.getUsername();
			data[i][1] = user.getPassword();
			data[i][2] = user.getName();
			data[i][3] = user.getPhone();
			data[i][4] = user.getType();
			i++;
		}
		DefaultTableModel tableModel = new DefaultTableModel(data, name);
		table = new JTable(tableModel);
		table.getTableHeader().setFont(new Font("宋体", Font.BOLD, 16)); // 表头字体
		table.setFont(new Font("微软雅黑", Font.PLAIN, 14)); // 表体字体
		table.getTableHeader().setPreferredSize(new Dimension(1, 30)); // 表头高度
		table.setRowHeight(table.getRowHeight() + 15); // 表体高度
		table.repaint();
		tablePanel = new JScrollPane(table);
		table.addMouseListener(new MouseAdapter() {
			public void mouseClicked(final MouseEvent e) {
				selRow = table.getSelectedRow(); // 得到选中的行
				if (selRow >= 0) {
					user = new User(userList.get(selRow).getUsername(),userList.get(selRow).getPassword(),userList.get(selRow).getName(),userList.get(selRow).getType(),userList.get(selRow).getPhone());
				}
			}

		});

		this.remove(scrollPane);
		scrollPane = tablePanel;
		scrollPane.setBounds(189, 120, 740, 478);
		this.add(scrollPane);
		this.validate();
	}

	/**
	 * @param 获得用户信息表格
	 * @return
	 */
	public JScrollPane getContactJPanel(List<User> userList) {
		String name[] = { "用户名", "密码", "姓名", "联系电话", "用户类型" };
		Object data[][] = new Object[userList.size()][5];
		Iterator<User> iter = userList.iterator();
		int i = 0;
		while (iter.hasNext()) {
			User user = iter.next();
			data[i][0] = user.getUsername();
			data[i][1] = user.getPassword();
			data[i][2] = user.getName();
			data[i][3] = user.getPhone();
			data[i][4] = user.getType();
			i++;
		}
		DefaultTableModel tableModel = new DefaultTableModel(data, name);
		table = new JTable(tableModel);
		table.getTableHeader().setFont(new Font("宋体", Font.BOLD, 16)); // 表头字体
		table.setFont(new Font("微软雅黑", Font.PLAIN, 14)); // 表体字体
		table.getTableHeader().setPreferredSize(new Dimension(1, 30)); // 表头高度
		table.setRowHeight(table.getRowHeight() + 15); // 表体高度
		table.repaint();
		tablePanel = new JScrollPane(table);
		table.addMouseListener(new MouseAdapter() {
			public void mouseClicked(final MouseEvent e) {
				selRow = table.getSelectedRow(); // 得到选中的行
				if (selRow >= 0) {
					user = new User(userList.get(selRow).getUsername(),userList.get(selRow).getPassword(),userList.get(selRow).getName(),userList.get(selRow).getType(),userList.get(selRow).getPhone());
				}
			}

		});

		return tablePanel;
	}

	/**
	 * @param 查询功能
	 */
	public void bookSearchActionPerformed(ActionEvent evt) {
		if (searchTypeComboBox.getSelectedItem().equals("按照用户名查询")) {
			String bookNameInput = bookManageTextField.getText().trim();
			libraryService = new LibraryService();
			userList = libraryService.getUsername(bookNameInput);
		}
		if (searchTypeComboBox.getSelectedItem().equals("按照用户姓名查询")) {
			String NameInput = bookManageTextField.getText().trim();
			libraryService = new LibraryService();
			userList = libraryService.getName(NameInput);
		}
		if (searchTypeComboBox.getSelectedItem().equals("按照用户类型查询")) {
			String authorNameInput = bookManageTextField.getText().trim();
			libraryService = new LibraryService();
			userList = libraryService.getUserType(authorNameInput);
		}
		String name[] = { "用户名", "密码", "姓名", "联系电话", "用户类型" };
		Object data[][] = new Object[userList.size()][5];
		Iterator<User> iter = userList.iterator();
		int i = 0;
		while (iter.hasNext()) {
			User user = iter.next();
			data[i][0] = user.getUsername();
			data[i][1] = user.getPassword();
			data[i][2] = user.getName();
			data[i][3] = user.getPhone();
			data[i][4] = user.getType();
			i++;
		}
		DefaultTableModel tableModel = new DefaultTableModel(data, name);
		table = new JTable(tableModel);
		table.getTableHeader().setFont(new Font("宋体", Font.BOLD, 16)); // 表头字体
		table.setFont(new Font("微软雅黑", Font.PLAIN, 14)); // 表体字体
		table.getTableHeader().setPreferredSize(new Dimension(1, 30)); // 表头高度
		table.setRowHeight(table.getRowHeight() + 15); // 表体高度
		table.repaint();
		tablePanel = new JScrollPane(table);
		table.addMouseListener(new MouseAdapter() {
			public void mouseClicked(final MouseEvent e) {
				selRow = table.getSelectedRow(); // 得到选中的行
				if (selRow >= 0) {
					user = new User(userList.get(selRow).getUsername(),userList.get(selRow).getPassword(),userList.get(selRow).getName(),userList.get(selRow).getType(),userList.get(selRow).getPhone());
				}
			}

		});

		this.remove(scrollPane);
		scrollPane = tablePanel;
		scrollPane.setBounds(189, 120, 740, 478);
		this.add(scrollPane);
		this.validate();
	}
}
